// pages/map/map.js
var area = require("../../utils/area.js")
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'J3ZBZ-WWXKJ-57RFY-FR7UP-OMKPK-GMBSJ'
});
const time = require('../../utils/time.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    AreaList: area.default,
    latitude: '23.12463',
    longitude: '113.36199',
    markers: [],
    status: '',
    show: false,
    select: [
      [{
          name: '不限',
          value: 0,
          gt:0,
          lt:999999999
        },
        {
          name: '300万以下',
          value: 0,
          gt: 0,
          lt: 3000000,
        },
        {
          name: '300万-500万',
          value: 0,
          gt: 3000000,
          lt: 5000000,
        },
        {
          name: '500万-800万',
          value: 0,
          gt: 5000000,
          lt: 8000000,
        },
        {
          name: '800万-1200万',
          value: 0,
          gt: 8000000,
          lt: 12000000,
        },
        {
          name: '1200万以上',
          value: 0,
          gt: 12000000,
          lt: 9999999999999,
        },
      ],
      [{
          name: '不限',
          value: 1,
          gt:0,
          lt:999999999
        },
        {
          name: '50㎡以下',
          value: 1,
          gt: 0,
          lt: 50
        },
        {
          name: '50㎡-90㎡',
          value: 1,
          gt: 50,
          lt: 90
        },
        {
          name: '90㎡-130㎡',
          value: 1,
          gt: 90,
          lt: 130
        },
        {
          name: '130㎡以上',
          value: 1,
          gt: 130,
          lt: 99999999
        },
      ],
      [{
          name: '不限',
          value: 2,
          time:0
        },{
          name: '3天以内',
          value: 2,
          time:-3
        },
        {
          name: '7天以内',
          value: 2,
          time:-7
        },
        {
          name: '15天以内',
          value: 2,
          time:-15
        },
        {
          name: '30天以内',
          value: 2,
          time:-30
        },
      ]
    ],
    areaValue: '天河区',
    arerShow: false,
    detailShow: false,
    area:['广东省','广州市','天河区'],
    price:[0,9999999999],
    measure:[0,9999999999],
    time:null
  },
  positioning(e) {
    console.log(e)
    qqmapsdk.geocoder({
      address: e.address,
      region: e.region,
      success: function (res) {
        console.log(res);
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  api(data) { //请求
    var that = this;
    if(that.data.area && !data.city) {
      data.province= that.data.area[0];
      data.city = that.data.area[1];
      data.district= that.data.area[2];
    }
    if(that.data.measure){
      data['areaMin'] = that.data.measure[0]
      data['areaMax'] = that.data.measure[1]
    } 
    if(that.data.price) {
      data['priceMin'] = that.data.price[0]
      data['priceMax'] = that.data.price[1]
    }
    if(that.data.time){
      data.startTime = that.data.time
    }
    data.page_size = 100
    getApp().ports.api1({
      data: data
    }).then((res) => {
      var res = res.data.results;
      var mak = [];
      for (var i = 0; i < res.length; i++) {
        if(i === 0){
          that.setData({
            longitude: res[i].longitude,
            latitude: res[i].latitude,
          })
        }
        var obj = {
          id: i,
          longitude: res[i].longitude,
          latitude: res[i].latitude,
          iconPath: 'https://api.yipaiju.com/resources/images/wawa/Maphome.png',
          order_id: res[i].id,
          callout: {
            content:res[i].title,
            display:'ALWAYS',
            // bgColor:'#A8D4F8',
            // color:'#fff',
            padding:5,
            borderRadius:10
          },
          area: res[i].residentialQuarterName,
          width: 35,
          height: 35,
        };
        mak.push(obj)
      }
      that.setData({
        markers: mak
      })
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.data.latitude = res.latitude
        that.data.longitude = res.longitude
        console.log(res)
        that.setData({
          latitude: that.data.latitude,
          longitude: that.data.longitude,
          identity:wx.getStorageSync('identity')
        })
        var data = {
          latitude: res.latitude,
          longitude: res.longitude
        }
        getApp().ports.apicitydsec({
          data: data
        }).then((obj) => {
          var obj = obj.data;
          that.setData({
            area: [obj.province, obj.city, obj.district],
            areaValue:obj.district
          })
          console.log(obj)
          var data = {
            province: obj.province,
            city: obj.city,
            district: obj.district,
          }
          that.api(data)
        }).catch(() => {
          console.error('出啥错了？');
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
  cancel() {
    this.setData({
      show: false
    });
  },
  showitem(e) {
    var that = this;
    that.data.status = e.currentTarget.dataset.status;
    that.setData({
      status: that.data.status,
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onSelect(event) {
    var that = this;
    if (event.detail.value == 0) {
      that.data.price = [event.detail.gt,event.detail.lt]
      console.log(event.detail);
      var data = {
        'priceMax': event.detail.lt,
        'priceMin': event.detail.gt
      };
      that.api(data)
    } else if (event.detail.value == 1) {
      that.data.measure = [event.detail.gt,event.detail.lt]
      var data = {
        'areaMax': event.detail.lt,
        'areaMin': event.detail.gt,
      };
      that.api(data)
    }else if(event.detail.value == 2){
      // that.data.time = time.timeHandle(null, event.detail.time)
      // var data = {
      //   startTime : time.timeHandle(null, event.detail.time)
      // }
      if (event.detail.time != 0){
        that.data.time = time.timeHandle(null, event.detail.time)
      }else{
        that.data.time = null;
      }
      var data = {};
      that.api(data)
    }
    that.setData({
      show: false
    })
  },
  location() { //回到当前位置
    var that = this;
    let mpCtx = wx.createMapContext("map");
    mpCtx.moveToLocation();
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        qqmapsdk.reverseGeocoder({
          location: res.latitude+','+res.longitude, //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
          //get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
          success: function(res) {//成功后的回调
            var res = res.result;
            var data = {
              province:res.address_component.province,
              city: res.address_component.city,
              district:res.address_component.district,
            }
            that.setData({
              area: [data.province, data.city, data.district],
              areaValue:data.district
            })
            that.api(data)
          
          },
          fail: function(error) {
            console.error(error);
          }
        })
      }
    })
  },
  regionchange(e) { //省市区切换
    console.log(e.type)
  },
  markertap(e) { //点击标记点
    var that = this;
    console.log(that.data.markers[e.detail.markerId])
    var data = {
      id: that.data.markers[e.detail.markerId].order_id
    }
    if(that.data.identity){
      getApp().ports.fpxqAdmin({
        data: data
      }).then((res) => {
        var res = res.data;
        res.priceYuan = Math.floor((res.priceYuan / 10000));
        res.marketPriceYuan = Math.floor((res.marketPriceYuan / 10000));
        res.estimatePriceYuan = Math.floor((res.estimatePriceYuan / 10000));
        res.time = (app.timeFormat(res.startTime) || '') + '至' + (app.timeFormat(res.endTime)||'');
        res.tags = [{
          name: res.builtUpAreaSquareCentimeter + '㎡'
        }, {
          name: res.discountRate + '折'
        }, {
          name: res.stage
        }]
        res.url = '../details/details?status=0&id=' + res.id
        that.setData({
          detailShow: true,
          list: res
        })
      }).catch(() => {
        console.error('出啥错了？');
      });
     }else{
    getApp().ports.fpxq({
      data: data
    }).then((obj) => {
      var res = obj.data;
      console.log(obj)
      res.priceYuan = Math.floor((res.priceYuan / 10000));
      res.marketPriceYuan = Math.floor((res.marketPriceYuan / 10000));
      res.estimatePriceYuan = Math.floor((res.estimatePriceYuan / 10000));
      res.time = app.timeFormat(res.startTime) + '至' + app.timeFormat(res.endTime);
      res.tags = [{
        name: res.builtUpAreaSquareCentimeter + '㎡'
      }, {
        name: res.discountRate + '折'
      }, {
        name: res.stage
      }]
      res.url = '../details/details?status=0&id=' + res.id
      that.setData({
        detailShow: true,
        list: res
      })
    }).catch(() => {
      console.error('出啥错了？');
    });
  }
  },
  navTo: function (e) { //跳转
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  controltap(e) {
    console.log(e.detail.controlId)
  },
  AreaHide() {
    this.setData({
      arerShow: false
    })
  },
  showPopup() {
    this.setData({
      arerShow: true
    })
  },
  ArerCofirm(e) {
    var that = this;
    console.log(e.detail.values);
    that.data.areaValue = e.detail.values[2].name;
    that.setData({
      arerShow: false,
      areaValue: that.data.areaValue
    })
    var data = {
      province: e.detail.values[0].name,
      city: e.detail.values[1].name,
      district: e.detail.values[2].name,
    }
    that.data.area = [data.province,data.city,data.district]
    that.api(data)
  },
  detailHide() {
    this.setData({
      detailShow: false
    })
  },
})